
#DEPS:


#echo "The script you are running has basename `basename $0`, dirname `dirname $0`"
#echo "The present working directory is `pwd`"

SCRIPTS=`dirname $0`



#1)get fastq files,

usage="Usage: $0 --qseqDir=[qseqDir] --lanes=[lanes] --prefix=[prefix] --outdir=[outdir]\n
--qseqDir: directory where the compressed qseq files are\n
--lanes  : comma seperated list of lanes\n
--prefix :this will be put infront of output file names\n
--outdir : the location of the output diretory\n
--readlength: integer for the length of the reads defaults to 50 base-pairs
all arguments except readlength are MANDATORY!!\n"

READLENGTH=50
for i in $*
do
	case $i in
    	--qseqDir=*)
		QSEQDIR=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
		;;
    	--lanes=*)
		LANES=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
		;;
    	--prefix=*)
		PREFIX=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
		;;
    	--outdir=*)
		OUTDIR=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
		;;
    	--readlength=*)
		READLENGTH=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
		;;
    	*)
                # unknown option
		;;
  	esac
done


if [ -z "$QSEQDIR"  ] || [ -z "$LANES" ] || [ -z "$PREFIX" ] || [ -z "$OUTDIR" ] ; then
    echo -e $usage
    exit 1
fi




mkdir -p $OUTDIR

# get lanes from comma seperated lane list
arr=(${LANES//,/ })
l1=${arr[0]}
l2=${arr[1]}
l3=${arr[2]}
l4=${arr[3]}
l5=${arr[4]}
l6=${arr[5]}
l7=${arr[6]}


# HERE TO UNTAR THE QSEQ files
for x in ${arr[@]}
do
    ls ${QSEQDIR}/*_$x[_.]qseq.tgz >> $OUTDIR/used.files.txt
    tar -C $QSEQDIR -zxvf ${QSEQDIR}/*_$x[_.]qseq.tgz
done



# fastq files are not there make them
# make qseqPF
cat ${QSEQDIR}/s_[s${l1}${l2}${l3}${l4}${l5}${l6}${l7}]_1_????_qseq.txt | perl -ne 'print if($_=~/1$/)' > $QSEQDIR/${PREFIX}_qseqPF.txt

# rm ${QSEQDIR}/s_[s${l1}${l2}${l3}${l4}${l5}${l6}${l7}]_1_????_qseq.txt

# Generate useBases string (Y repeated readLength times)
readlength=${READLENGTH}
useBases=`echo $readlength | perl -lane '@a= ('Y') x $F[0]; print join("", @a);'`


# This is for the fastq workflow -- I want to move in thid direction
perl ${SCRIPTS}/qseq2fastq.pl $QSEQDIR/${PREFIX}_qseqPF.txt > $OUTDIR/${PREFIX}_fastqPF.txt

rm  ${QSEQDIR}/${PREFIX}_qseqPF.txt

